<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="分类" prop="catalogId">
        <category-cascader ref="catSelect" @change="catSelectChange"/>
      </el-form-item>
      <el-form-item prop="brandId">
        <span slot="label">
                <el-tooltip content="需要先指定所属分类" placement="top">
                <i class="el-icon-question"></i>
                </el-tooltip>
                品牌
              </span>
        <brand-select ref="brandSelect" :value="queryParams.brandId" @change="brandChange"/>
      </el-form-item>
      <el-form-item label="状态" prop="publishStatus">
        <el-select v-model="queryParams.publishStatus" clearable>
          <el-option
            v-for="dict in dict.type.gl_spu_status"
            :key="dict.value"
            :label="dict.label"
            :value="Number(dict.value)"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="关键字" prop="key">
        <el-input
          v-model="queryParams.key"
          placeholder="请输入关键字"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="list">
      <el-table-column label="编号" align="center" prop="id"/>
      <el-table-column label="名称" align="center" prop="spuName"/>
      <el-table-column label="分类" align="center" prop="catalogName"/>
      <el-table-column label="品牌" align="center" prop="brandName"/>
      <el-table-column label="状态" align="center">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.gl_spu_status" :value="scope.row.publishStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="重量" align="center" prop="weight"/>
      <el-table-column label="描述" align="center" prop="spuDescription"/>
      <el-table-column label="创建时间" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.createTime | localDate }}</span>
        </template>
      </el-table-column>
      <el-table-column label="修改时间" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.updateTime | localDate }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-caret-top"
            @click="upSpu(scope.row)"
            v-if="scope.row.publishStatus === 0"
          >上架
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-tickets"
            @click="handleSpec(scope.row)"
          >规格
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <el-dialog :title="title" :visible.sync="open" width="700px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import BrandSelect from '@/components/my/BrandSelect'

export default {
  name: 'index',
  dicts: ['gl_spu_status'],
  components: { BrandSelect },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 表格数据
      list: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        catalogId: undefined,
        brandId: undefined,
        publishStatus: undefined,
        key: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      uri: '/product/spuInfo'
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询列表 */
    getList() {
      this.loading = true
      this.$api.get(this.uri, this.queryParams).then(({ data }) => {
        this.list = data.records
        this.total = data.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        //
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.$refs.brandSelect.clear()
      this.$refs.catSelect.reset()
      this.handleQuery()
    },
    catSelectChange(arr) {
      this.queryParams.catalogId = undefined
      this.queryParams.brandId = undefined
      this.$refs.brandSelect.clear()
      if (arr.length) {
        // 获取选中的分类
        const catId = arr[arr.length - 1]
        this.queryParams.catalogId = catId
        // 查询对应的品牌
        this.$refs.brandSelect.getBrands(catId)
      }
    },
    brandChange(val) {
      this.queryParams.brandId = val
    },
    // 上架商品
    upSpu(row) {
      this.$modal.loading()
      this.$api.post('/product/spuInfo/up/' + row.id).then(resp => {
        this.getList()
        this.$modal.msgSuccess('操作成功')
      }).finally(() => {
        this.$modal.closeLoading()
      })
    },
    // 规格维护
    handleSpec(row) {
      this.$router.push({ path: `/product/mainte/spu/spec/${row.id}/${row.catalogId}/${row.catalogName}` })
    }
  }
}
</script>

<style scoped>
</style>
